package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.Date;

/**
 * 办公申请查询
 *
 * @author wu
 */
public class WorkRequestQueryFragment extends BaseFragment implements OnClickListener {

  private static final int REQUEST_STARTTIME = 0;
  private static final int REQUEST_FINISHTIME = 1;
  private static final int REQUEST_DATASTATUS = 2;
  private TextView mStartTime_workRequest;// 开始时间
  private TextView mEndTime_workRequest;// 结束时间
  private TextView mDataStatus_workRequest;// 数据状态
  private String mStartTime;
  private String mEndTime;
  private String mDataStatus;
  protected static final String VALUE_STATUS_ALL = "all";
  protected static final String VALUE_NEW = "new";
  protected static final String VALUE_WAITCHECK = "waitCheck";
  protected static final String VALUE_CHECKPASS = "checkPass";
  protected static final String VALUE_ABORT = "abort";
  public static final String REQUEST_STRAT_DATE = "com.isunland.managesystem.ui.REQUEST_STRAT_DATE";
  public static final String REQUEST_END_DATE = "com.isunland.managesystem.ui.REQUEST_END_DATE";
  public static final String REQUEST_DATA_STATUS = "com.isunland.managesystem.ui.EXTRA_DATASATUS";

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置返回图标
    setBack(true);
    setTitleCustom(R.string.workRequestQuery);
    mStartTime = getArguments().getString(REQUEST_STRAT_DATE);
    mEndTime = getArguments().getString(REQUEST_END_DATE);
    mDataStatus = getArguments().getString(REQUEST_DATA_STATUS);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_workrequest_query, container, false);
    mStartTime_workRequest = (TextView) view.findViewById(R.id.tv_startTime_workRequest);
    mEndTime_workRequest = (TextView) view.findViewById(R.id.tv_endTime_workRequest);
    mDataStatus_workRequest = (TextView) view.findViewById(R.id.tv_dataStatus_workRequest);

    mStartTime_workRequest.setText(mStartTime);
    mEndTime_workRequest.setText(mEndTime);

    if (TextUtils.isEmpty(mDataStatus)) {
      mDataStatus_workRequest.setText(getString(R.string.all));
    } else {
      if (VALUE_NEW.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.draft);
      } else if (VALUE_WAITCHECK.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.checking);
      } else if (VALUE_CHECKPASS.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.checkapprovve);
      } else if (VALUE_ABORT.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.stopup);
      }
    }

    mStartTime_workRequest.setOnClickListener(this);
    mEndTime_workRequest.setOnClickListener(this);
    mDataStatus_workRequest.setOnClickListener(this);
    return view;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.tv_startTime_workRequest:
        showDialog(REQUEST_STARTTIME, new Date());
        break;
      case R.id.tv_endTime_workRequest:
        showDialog(REQUEST_FINISHTIME, new Date());
        break;
      case R.id.tv_dataStatus_workRequest:
        showDialog(REQUEST_DATASTATUS, new Date());
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_STARTTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_FINISHTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_DATASTATUS:
        dialog = new DeliveryDataStatusdialogFragment();
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(WorkRequestQueryFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_STARTTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mStartTime_workRequest.setText(mStartTime);
    }

    if (requestCode == REQUEST_FINISHTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mEndTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mEndTime_workRequest.setText(mEndTime);
      return;
    }

    if (requestCode == REQUEST_DATASTATUS && data != null) {
      mDataStatus = data.getStringExtra(DeliveryDataStatusdialogFragment.EXTRA_DATA_STATUS);
      LogUtil.i("mDataStatus===" + mDataStatus);
      if (VALUE_NEW.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.draft);
      } else if (VALUE_WAITCHECK.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.checking);
      } else if (VALUE_CHECKPASS.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.checkapprovve);
      } else if (VALUE_ABORT.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.stopup);
      } else if (VALUE_STATUS_ALL.equalsIgnoreCase(mDataStatus)) {
        mDataStatus_workRequest.setText(R.string.all);
        mDataStatus = "";
      }
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        if (MyUtils.isEmptyText(mStartTime_workRequest)
            && MyUtils.isEmptyText(mEndTime_workRequest)
            && MyUtils.isEmptyText(mDataStatus_workRequest)) {
          ToastUtil.show(R.string.chooseQueryOption);
        } else if (MyDateUtil.startBeforeEnd(mEndTime_workRequest.getText().toString().trim(),
            mStartTime_workRequest.getText().toString().trim())) {
          ToastUtil.show(R.string.startTimeBeforeEndTime);
        } else {
          Intent intent = new Intent();
          String startTime = mStartTime_workRequest.getText().toString().trim();
          String endTime = mEndTime_workRequest.getText().toString().trim();

          intent.putExtra(WorkRequestListFragment.REQUEST_STARTTIME, startTime);
          intent.putExtra(WorkRequestListFragment.REQUEST_ENDTIME, endTime);
          intent.putExtra(WorkRequestListFragment.REQUEST_DATASTATUS, mDataStatus);
          getActivity().setResult(Activity.RESULT_OK, intent);
          getActivity().finish();
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public static Fragment newInstance(String start, String end, String status) {
    Bundle bundle = new Bundle();
    bundle.putString(REQUEST_STRAT_DATE, start);
    bundle.putString(REQUEST_END_DATE, end);
    bundle.putString(REQUEST_DATA_STATUS, status);
    WorkRequestQueryFragment fragment = new WorkRequestQueryFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
